<!--意见反馈管理-->
<template>
  <div id="upmsFeedback" class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item>
          <el-input v-model="listQuery.keyword" placeholder="请输入关键字" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button plain type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
          <el-button plain icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--列表-->
    <div class="table-container flex-main">
      <div class="table-container-header">
        <table-toolbar :create="false" :remove="false" @refresh="handleFilter" />
      </div>
      <el-table
        ref="multipleTable"
        :data="list"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        @sort-change="sortChange">
        <el-table-column type="selection" width="60" />
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="分类" prop="category" width="100" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row._category }}</template>
        </el-table-column>
        <el-table-column label="标题" prop="title" min-width="300" sortable show-overflow-tooltip>
          <template slot-scope="scope"><span class="color-primary common-cursor" @click="handleLookDetails(scope.row)">{{ scope.row.title }}</span></template>
        </el-table-column>

        <el-table-column label="回复" prop="isReply" width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.isReply=== 0 " class="color-danger ">
              未回复
              &nbsp;&nbsp;
              <button-option icon="el-icon-setting" skin="danger" title="去回复" @click="handleDealReply(scope.row)" />
            </span>
            <span v-else class="color-primary">
              已回复
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="createdBy" align="center" label="反馈人" width="180" :show-overflow-tooltip="true">
          <template slot-scope="scope">{{ scope.row.createdBy | optionUserFormat }}</template>
        </el-table-column>
        <el-table-column v-if="false" label="操作" align="center" fixed="right" width="90">
          <template slot-scope="scope">
            <button-option v-if="scope.row.isReply=== 0 " icon="el-icon-edit" skin="primary" title="编辑" @click="handleUpdate(scope.row)" />
            <button-option icon="el-icon-delete" skin="danger" title="删除" @click="handleRemoveSingle(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>

    <!--新增/编辑侧边栏-->
    <side-box :show="sideBox.recordShow" :title="sideBox.title" @close="sideBox.recordShow = !sideBox.recordShow">
      <template slot="body">
        <el-form
          ref="dataForm"
          :rules="upmsFeedbackRules"
          :model="upmsFeedbackRecord"
          label-position="right"
          label-width="120px">
          <el-form-item label="标题:" prop="title">
            <el-input v-model="upmsFeedbackRecord.title" maxlength="40" show-word-limit placeholder="请输入" />
          </el-form-item>
          <el-form-item label="分类:" prop="category">
            <data-dict-select v-model="upmsFeedbackRecord.category" :code="'feedbackCategory'" placeholder="请选择" />
          </el-form-item>

          <el-form-item label="意见反馈:" prop="content">
            <el-input
              v-model="upmsFeedbackRecord.content"
              :rows="4"
              type="textarea"
              maxlength="1000"
              show-word-limit
              placeholder="请输入" />
          </el-form-item>
          <el-form-item label="图片:">
            <MultipleImageUpload
              v-model="upmsFeedbackRecord.images"
              :catalog="constants.FILE_CATALOG_FEEDBACK"
              :limit="5"
              :crop="false"
              desc="单张照片不能超过2M，最多只允许上传5张照片" />
          </el-form-item>
          <el-form-item label="小视频:">
            <SimpleUploader
              v-model="upmsFeedbackRecord.tempVideos"
              text="上传视频"
              accept="audio/mp4, video/mp4"
              :catalog="constants.FILE_CATALOG_FEEDBACK" />
          </el-form-item>
        </el-form>
      </template>
      <template slot="foot">
        <el-button type="primary" plain @click="sideBox.status==='create'?createData():updateData()">保存 </el-button>
      </template>
    </side-box>
    <model-dialog
      title="意见反馈回复"
      :visible.sync="dealDialogVisible"
      width="600px"
      @close="dealDialogVisible=!dealDialogVisible">
      <el-form
        ref="dataForm"
        :model="upmsFeedbackRecord"
        label-width="120px">
        <el-form-item label="标题:" prop="title">
          <el-tag>{{ upmsFeedbackRecord._category }}</el-tag>
          {{ upmsFeedbackRecord.title }}
        </el-form-item>
        <el-form-item label="描述:" prop="content">
          <span class="color-danger">{{ upmsFeedbackRecord.content }}</span>
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.images" label="图片:">
          <FileItem :files="upmsFeedbackRecord.images.split(',')" />
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.videos" label="小视频:">
          <FileItem :files="upmsFeedbackRecord.videos.split(',')" />
        </el-form-item>
        <el-form-item label="回复内容:" prop="replyContent">
          <el-input
            v-model="upmsFeedbackRecord.replyContent"
            :rows="6"
            type="textarea"
            maxlength="1000"
            show-word-limit
            placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="handleReply">回 复</el-button>
      </template>
    </model-dialog>
    <model-dialog
      :title="'【'+upmsFeedbackRecord.title+'】详情'"
      :visible.sync="detailsDialogVisible"
      width="600px"
      @close="detailsDialogVisible=!detailsDialogVisible">
      <el-form
        ref="dataForm"
        :model="upmsFeedbackRecord"
        label-width="120px">
        <el-form-item label="标题:">
          <el-tag>{{ upmsFeedbackRecord._category }}</el-tag>
          {{ upmsFeedbackRecord.title }}
        </el-form-item>
        <el-form-item label="反馈人:" prop="createdBy">
          {{ upmsFeedbackRecord.createdBy | optionUserFormat }}
        </el-form-item>
        <el-form-item label="描述:">
          <span class="color-danger">{{ upmsFeedbackRecord.content }}</span>
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.images" label="图片:">
          <FileItem :files="upmsFeedbackRecord.images.split(',')" />
        </el-form-item>
        <el-form-item v-if="upmsFeedbackRecord.videos" label="小视频:">
          <FileItem :files="upmsFeedbackRecord.videos.split(',')" />
        </el-form-item>
        <el-form-item label="回复:">
          <span v-if="upmsFeedbackRecord.isReply=== 0 " class="color-danger ">未回复</span>
          <span v-else class="color-primary">{{ upmsFeedbackRecord.replyContent }} </span>
        </el-form-item>
        <template v-if="upmsFeedbackRecord.isReply===1 ">
          <el-form-item label="回复时间:">
            {{ upmsFeedbackRecord.replyPeople | optionUserFormat }}
          </el-form-item>
          <el-form-item label="回复时间:">
            {{ upmsFeedbackRecord.replyTime | formatDate }}
          </el-form-item>
        </template>

      </el-form>
    </model-dialog>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      },

      multipleSelectionRecords: [],
      // 固定写法
      sideBox: {
        recordShow: false,
        title: '',
        status: 'create'
      },
      upmsFeedbackRecord: {},
      upmsFeedbackRules: {
        createdBy: [
          { required: true, message: '请输入创建者', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请输入分类', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      dealDialogVisible: false,
      detailsDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 表格自带方法开始
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格行数据单击*/
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    /* 搜索*/
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /**
     * 重置
     */
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      }
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('upmsFeedbackPage', this.listQuery)
      this.list = response.records
      this.total = response.total
    },
    /* 新增*/
    handleCreate() {
      this.upmsFeedbackRecord = {
        tempVideos: []
      }
      this.sideBox.status = 'create'
      this.sideBox.recordShow = true
      this.sideBox.title = '新增意见反馈'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          var temp = Object.assign({}, this.upmsFeedbackRecord)
          temp = {
            ...temp,
            videos: temp.tempVideos.join(',')
          }
          delete temp.tempVideos
          this.$store.dispatch('upmsFeedbackSave', temp).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('创建成功')
            this.handleReset()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },
    /* 编辑*/
    handleUpdate(row) {
      this.upmsFeedbackRecord = Object.assign({}, row) // copy obj
      this.upmsFeedbackRecord = {
        ... this.upmsFeedbackRecord,
        tempVideos: this.upmsFeedbackRecord.videos ? this.upmsFeedbackRecord.videos.split(',') : []
      }
      this.sideBox.status = 'update'
      this.sideBox.recordShow = true
      this.sideBox.title = '编辑意见反馈'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          var tempData = Object.assign({}, this.upmsFeedbackRecord)
          tempData = {
            ...tempData,
            videos: tempData.tempVideos.join(',')
          }
          delete tempData.tempVideos
          this.$store.dispatch('upmsFeedbackUpdate', tempData).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('修改成功')
            this.handleFilter()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },

    /* 删除*/
    handleRemoveSingle(row) {
      this.multipleSelectionRecords = []
      this.multipleSelectionRecords.push(row)
      this.$refs.multipleTable.clearSelection()
      this.$refs.multipleTable.toggleRowSelection(row, true)
      this.removeRecord()
    },
    removeRecord() {
      if (this.multipleSelectionRecords.length > 0) {
        const ids = []
        this.multipleSelectionRecords.forEach(function(v, i) {
          ids.push(v.id)
        })
        this.$confirm('此操作将永久删除记录, 是否继续?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('upmsFeedbackRemove', ids).then(response => {
            this.handleReset()
            this.$modal.msgSuccess('删除成功')
          }).catch(() => {})
        }).catch(() => { })
      } else {
        this.$modal.msgWarning('请选择要删除的记录')
      }
    },

    handleLookDetails(row) {
      this.upmsFeedbackRecord = Object.assign({}, row) // copy obj
      this.detailsDialogVisible = true
    },
    handleDealReply(row) {
      this.upmsFeedbackRecord = Object.assign({}, row) // copy obj
      this.dealDialogVisible = true
    },
    handleReply() {
      if (!this.upmsFeedbackRecord.replyContent) {
        this.$modal.msgWarning('请输入回复内容!')
        return
      }
      this.$store.dispatch('upmsFeedbackReply', this.upmsFeedbackRecord).then(response => {
        this.dealDialogVisible = false
        this.$modal.msgSuccess('回复成功')
        this.handleFilter()
      }).catch(() => {
        this.dealDialogVisible = true
      })
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
